Ext.namespace('iOS.Content');

Ext.define('AppContent', {
    extend   : 'ContentView',
    
    statics  : {
        ViewCls      : 'app-content',
        screenIframe : 'screen-iframe',
        TopBar       : 'top-bar',
        GlobalContentName : 'content'
    },
    
    /*
     * app状态，false表示关闭。true表示开启
     */
    _appState     : {
        app       : 'normal',
        iframe    : 'normal',
        topBar    : 'normal'
    },
    
    /*
     * 是否为全屏显示，默认为不全屏
     */
    isFullScreen  : false,

    /*
     *  表明当前是否为iframe状态
     */
    _isIframe     : false,
    
    _contentTop   : null,

    /*
     * 当前打开的APP的名字
     */
    _curAppName   : null,    

    _eventList: [
        ['runApp'],
        ['runIframe'],
        ['closeApp'],
        ['reAddApp']
    ],

    _initView: function(){
        this.callParent();
        var topBar       = iOS.ViewManager.getView(this.self.TopBar);
    },

    isIframe: function(){
        return this._isIframe;
    },

    getCurAppName: function(){
        return this._curAppName;
    },

    runIframe: function(appName, src, isFullScreen, topBarColor){
        if(this._appState.iframe == 'opening'){
            return;
        }
        this._curAppName = appName; 
        this._isIframe   = true;
        var height       = isFullScreen ? 480 : 460,
            sttc         = this.self,
            htmlData     = 
                '<iframe src="' + src + '" class="' + sttc.screenIframe + '"><iframe>';
        this.getElCache().css('height', height + 'px').
            html(htmlData).cssScale(1);
        this.appEnvironment(isFullScreen, topBarColor);
        this._appState.iframe = 'opening';
    },

    onDestroy : function(){
        this.getElCache().on('webkitTransitionEnd', function(){
            /*
             * @FIXME:若某icon内没有内容，则当下一次点击一个icon时，icon运动结束会触发，
             * 目前当按下home键时，不会清除Dom节点，当没有空icon时，可将此注释删除
             */
            $(this).off('webkitTransitionEnd').html('');
        });
    },
    
    /*
     * 适用于当App有缓存的时候
     */
    reAddApp: function(appName){
        var backStage = iOS.BackStage,
            app       = backStage.getApp(appName);
        app.appendTo(this.getElCache());
    },

    runApp: function(appName, isFullScreen, topBarColor, GlobalBg){
        if(this._appState.app == 'opening'){
            return;
        }
        var that            = this;
        this._curAppName    = appName;
        this.getElCache().on('webkitTransitionEnd', function(){
            that.getElCacheByCls('x-scroller-parent').css('overflow', 'visible');
            $(this).off('webkitTransitionEnd');
        }).cssScale(1);
        setTimeout(function(){
            if(GlobalBg && GlobalBg != 'transparent' && GlobalBg != ''){
                iOS.ViewManager.getView(that.self.GlobalContentName)
                    .getElCache().css('backgroundColor', GlobalBg);
            }
        }, 450);
        this.appEnvironment(isFullScreen, topBarColor);
        this._appState.app  = 'opening';
    },

    /*
     * @FIXME:关闭app，目前仅仅是清除了Dom节点，各个App的实例化对象依然保存在viewmanager里面，
     * 下一次打开相同App时会刷新viewmanager中相应的对象.
     */
    closeApp: function(){
        if(!this._appState.app == 'normal'){
            return;
        }
        if(this._appState.topBar == 'hide'){
            this.getElCache().css('top', '20px');
        }
        var that = this;
        setTimeout(function(){
            that.getElCacheByCls('x-scroller-parent').css('overflow', 'hidden');
        }, 150);
        this.getElCache().cssScale(0);
        this.onDestroy();
        for(var i in this._appState){
            this._appState[i] = 'normal';
        }
    },

    /*
     * 配置app运行环境
     */
    appEnvironment: function(isFullScreen, topBarColor, GlobalBg){
        if(isFullScreen){
            this.getElCache().css('top', '0');
            iOS.ViewManager.notify(this.self.TopBar, 'hideTopBar');
            this._appState.topBar = 'hide';
        }
        switch(topBarColor)
        {
            case 'black' :
                iOS.ViewManager.notify(this.self.TopBar, 'bgBlack');
                break;
            case 'white' :
                iOS.ViewManager.notify(this.self.topBar, 'bgWhite');
                break;
            default : 
        }
    }
});

iOS.Content.AppContent = AppContent;
